var product = {
	newReleaseProductArray:new Array(),
	
	currentNewReleaseProductIndex:0,
	
	currentCategory:0,
	
    currentProductPageNo:0,
    
	categoriesListByParentId:function(parentId){
    	$.ajax({
            type: "GET",
            url: config.baseURL + "products/categories/list.json",
            cache:false,
            dataType: "json",
            data : {"parentId":parentId},
            success: function(data){
                if(data.result == 'success'){
        	    	$.each(data.categoriesList, function(index, obj) {
        				var html = "<li><a href='"+config.baseURL+"products?category="+obj.id+"' class='wrap_productImg'>" +
        					"<img src='"+config.baseURL+obj.imagePath+"'/></a>"+
							"<div class='cateIntro'>"+
								"<h3 class='cateName'><a href='"+config.baseURL+"products?category="+obj.id+"'>"+obj.subject+"</a></h3>"+
								"<p>"+obj.description+"</p>"+
								"<a href='"+config.baseURL+"products?category="+obj.id+"' class='moreBtn'>MORE</a>"+
							"</div></li>";

        				$('#productRootCategories').append(html);
        			});
                }else{
                    $.msgBox.alert({
                        title:'Tips',
                        msg:'System error!Please try again later!',
                        icon:'warning',
                        width:350
                    });
                }
            }
        });
    },
    categoriesListTree:function(referId){
        $.ajax({
            type: "GET",
            url: config.baseURL + "products/categories/listTree",
            cache:false,
            dataType: "json",
            success: function(data){
                if(data.result == 'success'){
                	var currentCategoryId = utils.getUrlParameter("category");
                	
                    $.each(data.categoriesList, function(index, obj) {
                    	var selectedClass = "";
                    	if(currentCategoryId == obj.id){
                    		selectedClass = " class='selected'";
                    		$('#categoryPath').html(obj.subject);
                    	}
                        var html = '<dt id="category'+obj.id+'" '+selectedClass+'><a href="'+config.baseURL+'products?category='+obj.id+'">'+obj.subject+'</a></dt>';
                        $.each(obj.categoriesList, function(index, obj) {
                        	selectedClass = "";
                            if(currentCategoryId == obj.id){
                                selectedClass = " class='selected'";
                                $('#categoryPath').html(obj.subject);
                            }
                            html += '<dd id="category'+obj.id+'" '+selectedClass+'><a href="'+config.baseURL+'products?category='+obj.id+'">'+obj.subject+'</a></dd>';
                        });
                        
                        $('#'+referId).append(html);
                    });
                }else{
                    $.msgBox.alert({
                        title:'Tips',
                        msg:'System error!Please try again later!',
                        icon:'warning',
                        width:350
                    });
                }
            }
        });
    },
    list:function(page){
        $.ajax({
            type: "GET",
            url: config.baseURL + "products/searchProducts.json",
            cache:false,
            dataType: "json",
            data : {"newRelease":-1,"category":product.currentCategory,"currentPage":page + 1,"perPageSize":9},
            success: function(data){
                if(data.result == 'success'){
                    $('#productList').html("");

                    $.each(data.productList, function(index, obj) {
                        var html = '<li><a href="'+config.baseURL+'products/'+obj.id+'">' +
                                '<img src="'+config.baseURL+obj.imagePath+'"/></a>'+obj.name+'</li>';
                            
                        $('#productList').append(html);
                    });
                    
                    if(data.productListCount==0){
                        $('#productList').append('<li class="nodata">nodata</li>');
                    }else{
                        $("#paginationDiv").pagination(data.productListCount, {
                            callback: product.list,//回调函数
                            num_display_entries: 20,
                            num_edge_entries: 12,
                            items_per_page:9,//每页显示多少条
                            current_page:page,//当前页码
                            prev_show_always:false,
                            next_show_always:false
                        });
                    }
                    
                    if(data.productListCount>9){
                        $("#paginationDiv").show();
                    }else{
                        $("#paginationDiv").hide();
                    }
                    
                    currentProductPageNo = page;
                }else{
                    $.msgBox.alert({
                        title:'Tips',
                        msg:'System error!Please try again later!',
                        icon:'warning',
                        width:350
                    });
                }
            }
        });
    },
    newRelease:function(){
        $.ajax({
            type: "GET",
            url: config.baseURL + "products/searchProducts.json",
            cache:false,
            dataType: "json",
            data : {"newRelease":1,"currentPage":1,"perPageSize":100},
            success: function(data){
                if(data.result == 'success'){
                	if(data.productList.length>0){
                        product.newReleaseProductArray = data.productList;
                        product.currentNewReleaseProductIndex = 0;
                        
                        var obj = product.newReleaseProductArray[0];
                        
                        $('#currentNewReleaseProductName').html(obj.name);
                        $('#currentNewReleaseProductLink').attr("href",config.baseURL+"products/"+obj.id);
                        $('#currentNewReleaseProductImage').attr("src",config.baseURL+obj.imagePath).fadeIn();
                	}
                }else{
                    $.msgBox.alert({
                        title:'Tips',
                        msg:'System error!Please try again later!',
                        icon:'warning',
                        width:350
                    });
                }
            }
        });
    },
    switchCurrenNewReleaseProduct:function(movePosition){
    	var afterPosition = product.currentNewReleaseProductIndex+movePosition;
    	if(afterPosition<0){
    	    afterPosition = product.newReleaseProductArray.length-1;
    	}else if(afterPosition > product.newReleaseProductArray.length-1){
    	    afterPosition = 0;
    	}
        product.currentNewReleaseProductIndex = afterPosition;
    	
        var obj = product.newReleaseProductArray[afterPosition];
        
        $('#currentNewReleaseProductName').html(obj.name);
        $('#currentNewReleaseProductImage').hide().attr("src",config.baseURL+obj.imagePath).fadeIn();
        $('#currentNewReleaseProductLink').attr("href",config.baseURL+"products/"+obj.id);
        
    },
    detailInfo:function(productId){
       $.ajax({
            type: "GET",
            url: "../products/detail.json",
            cache:false,
            dataType: "json",
            data : {"id":productId},
            success: function(data){
                if(data.result == 'success'){
                    var obj = data.product;
                    $('#productPath').html(obj.productCategories.subject);
                    $('#productNumber').html(obj.number);
                    $('#productName').html(obj.name);
                    $('#productSpec').html(obj.spec);
                    $('#productImage').attr("src",config.baseURL+obj.imagePath);
                    $('#productReadmeInfo').html(obj.productReadmeInfo);
                }else{
                    $.msgBox.alert({
                        title:'Tips',
                        msg:'System error!Please try again later!',
                        icon:'warning',
                        width:350
                    });
                }
            }
        });
    }
};